package com.im.project.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.im.common.constant.Constant;
import com.im.common.util.SpringContextHolder;
import com.im.project.entity.ChatMessage;
import com.im.project.entity.vo.MessageVo;
import com.im.project.entity.dto.UpdateChatUserMessageDto;
import com.im.project.service.impl.ChatMessageServiceImpl;
import com.im.project.service.impl.ChatServiceImpl;
import com.im.project.webSocket.ProcessFactory;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * mq消息消费
 *
 * @author yunZhouJiaDi
 */
@Slf4j
@Component
public class RabbitmqConsumer {

    @RabbitListener(queues = {Constant.RabbitmqConstant.QUEUE_SYS_MESSAGE})
    public void watchQueue(String payload, Channel channel, Message message) throws IOException {
        String messageId = message.getMessageProperties().getMessageId();
        String msg = new String(message.getBody(), "utf-8");
        RedisTemplate redisTemplate = SpringContextHolder.getBean("redisTemplate");
        String messageRedisValue = (String) redisTemplate.opsForValue().get(Constant.RedisConstant.MQ_SEND_MESSAGE + messageId);
        if (msg.equalsIgnoreCase(messageRedisValue)) {
            return;
        }
        redisTemplate.opsForValue().set(Constant.RedisConstant.MQ_SEND_MESSAGE + messageId, msg, 1L, TimeUnit.MINUTES);
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        try {
            switch (routingKey) {
                case Constant.RabbitmqConstant.ROUTING_KEY + Constant.RabbitmqConstant.SEND_MESSAGE:
                    MessageVo messageVo = JSONObject.parseObject(payload, MessageVo.class);
                    ProcessFactory.process(messageVo, messageVo.getContactId());
                    break;
                case Constant.RabbitmqConstant.ROUTING_KEY + Constant.RabbitmqConstant.UPDATE_MESSAGE:
                    UpdateChatUserMessageDto updateChatUserMessageDto = JSONObject.parseObject(payload, UpdateChatUserMessageDto.class);
                    ChatServiceImpl chatServiceImpl = SpringContextHolder.getBean("chatServiceImpl");
                    chatServiceImpl.updateChat(updateChatUserMessageDto);
                    break;
                case Constant.RabbitmqConstant.ROUTING_KEY + Constant.RabbitmqConstant.SAVE_MESSAGE:
                    ChatMessage chatMessage = JSONObject.parseObject(payload, ChatMessage.class);
                    ChatMessageServiceImpl chatMessageServiceImpl = SpringContextHolder.getBean("chatMessageServiceImpl");
                    chatMessageServiceImpl.save(chatMessage);
                    break;
                default:
                    break;
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("mq消费消息成功");
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                log.warn("消息已重复处理失败，拒绝再次接受");
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                log.info("消息即将返回队列处理");
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }
}
